import 'package:flutter/material.dart';
import 'package:zy_pos_demo/pages/operator.dart';
import 'package:zy_pos_demo/resources/res.dart';

class MainPage extends StatefulWidget {
  @override
  _MainPageState createState() => _MainPageState();
}

class _MainPageState extends State<MainPage> {
  List<String> titles = [];
  List page = [];
  PageController _pageController;
  int _currentPage = 0;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    page.add(OperatePage());
    page.add(Text("设置"));
    titles.add("操作台");
    titles.add("设置");
    _pageController = PageController(initialPage: 0);
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(
          title: Center(
            child: Text(
              titles[_currentPage],
              style: TextStyle(
                fontSize: 21,
                color: Colors.white,
              ),
            ),
          ),
          backgroundColor: AppColors.BLACK_3,
        ),
        body: PageView.builder(
          itemBuilder: (context, index) {
            return page[index];
          },
          controller: _pageController,
          itemCount: page.length,
          onPageChanged: (value) {
            if (_currentPage != value) {
              setState(() {
                _currentPage = value;
              });
            }
          },
        ),
        bottomNavigationBar: BottomNavigationBar(
          currentIndex: _currentPage,
          fixedColor: Colors.white,
          unselectedItemColor: AppColors.GREY_C,
          backgroundColor: AppColors.BLACK_3A,
          items: [
            BottomNavigationBarItem(
              label: titles[0],
              icon: Icon(Icons.open_with),
            ),
            BottomNavigationBarItem(
              label: titles[1],
              icon: Icon(Icons.settings),
            )
          ],
          onTap: _onTap,
        ),
      ),
    );
  }

  _onTap(int index) {
    _pageController.animateToPage(
      index,
      duration: const Duration(milliseconds: 500),
      curve: Curves.easeIn,
    );
  }
}
